﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments
{
    class MSA
    {
        public StringBuilder Acknowledgement_code { get; set; }
        public StringBuilder Message_Control_ID { get; set; }
        public StringBuilder Text_Message { get; set; } // if there is an error
        public bool required { get; set; }

          enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }
          public MSA()
          {
              Acknowledgement_code = new StringBuilder();
              Message_Control_ID = new StringBuilder();
              Text_Message = new StringBuilder();
          }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }


        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (source[i] == '|')
                {
                    {
                        x++;
                        continue;
                    }
                }
                else
                {
                    if (x == 1)  // passing prameter of set id - PID
                    {
                        if (source[i] != '|')
                        {
                            if (Acknowledgement_code == null)
                                Acknowledgement_code = new StringBuilder();
                            Acknowledgement_code.Insert(Acknowledgement_code.Length, source[i]);
                        }
                    }
                    else if (x == 2)
                    {
                        if (source[i] != '|')
                        {
                            if (Message_Control_ID == null)
                                Message_Control_ID = new StringBuilder();
                            Message_Control_ID.Insert(Message_Control_ID.Length, source[i]);
                        }
                    }
                    else if (x == 3)
                    {
                        if (check(source, i) != null)
                        {
                            Acknowledgement_code.Capacity = Acknowledgement_code.Length;
                            Message_Control_ID.Capacity = Message_Control_ID.Length;
                            Text_Message.Capacity = Text_Message.Length;
                            return i - 1;
                        }

                    else    if (source[i] != '|')
                        {
                            if (Text_Message == null)
                                Text_Message = new StringBuilder();
                            Text_Message.Insert(Text_Message.Length, source[i]);
                        }
                    }
                }
            }
            return i;
        }

        public string tostring()
        {
            string msa_string = "MSH";
       
            if (Acknowledgement_code.Length != 0)
                msa_string += "|" + Acknowledgement_code;
            if (Message_Control_ID.Length != 0)
                msa_string += "|" + Message_Control_ID;
            if (Text_Message.Length != 0)
                msa_string += "|" + Text_Message;           
            return msa_string;
        }
    }
}
